package org.jackysoft.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.jackysoft.entity.AcegiRole;
import org.jackysoft.entity.TreeNode;
import org.jackysoft.entity.User;
import org.jackysoft.opensymphony.workflow.SecurityUsersWork;
import org.jackysoft.service.AbstractIdentifiedService;
import org.jackysoft.service.SecurityRoleService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;


@Service
@Transactional
public class SecurityRoleServiceImpl 
extends AbstractIdentifiedService<AcegiRole>
implements SecurityRoleService{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8536413511276812598L;
    private static final String CONSTRUCT_STAT = " new AcegiRole(a.id , a.name) ";
	
	public SecurityRoleServiceImpl() {
		this.type = AcegiRole.class;
	}

	@Override
	public void create(AcegiRole r) throws DataAccessException{
	  
		super.create(r);
	   
	}
	
	@Override
	public void update(AcegiRole a) throws DataAccessException {
		
				
		Collection<TreeNode> trees = a.getTreeNodes();
		     		
		Set<TreeNode> nodes = Sets.newHashSet();
		
		nodes.addAll(trees); 	   
	    
		a.setTreeNodes(nodes);
	    
		a.setEnabled(true);	   
		
		super.update(a);		
	
	}

	@Override
	public Collection<AcegiRole> getsByUrl(String treeid)
			throws DataAccessException {
		Session sess = getSession();
		String hql = "select "+CONSTRUCT_STAT+" from AcegiRole  a join a.treeNode as tree where tree.id=:treeid";
	    Query q = sess.createQuery(hql).setString("treeid", treeid);
		return q.list();
	}

	@Override
	public Collection<AcegiRole> gets(final TreeNode node)
			throws DataAccessException {
		String hql="select distinct  "+CONSTRUCT_STAT+"  from AcegiRole a join a.treeNode tns where tns in (:tree)";
		Collection<TreeNode> trees = new ArrayList<TreeNode>();
		trees.add(node);
		Session sess = getSession();
		Collection<AcegiRole> alist = sess.createQuery(hql)
		                             .setParameterList("tree", trees)
		                             .list();
	
		return alist;
	}
		

	@Override
	public Collection<AcegiRole> gets(User user)
			throws DataAccessException {
		Session sess = getSession();
		User u = (User) sess.get(User.class, user.getUsername());
		if(u==null) return Lists.newArrayList();
        return u.getAuthoritys();
	}

	
	@Override
	public int[] appendUsers(AcegiRole bean, Collection<User> users)
			throws DataAccessException {
		if(bean==null || users==null || bean.getName()==null) return new int[0];
		Collection<User> targetUsers = Sets.newHashSet();
		targetUsers.addAll(users);
		getSession().doWork(new SecurityUsersWork(bean,targetUsers));
		return null;
	}
	
	
	boolean hasRole(User user,AcegiRole role){
		if(role==null || user==null ) return false; 
			  //throw new IllegalArgumentException(" 角色不能为空 role "+ role);
		
		Collection<AcegiRole> roles = user.getAuthoritys();
		if(roles==null) return false;
		for(AcegiRole ar:roles){
			if(role.equals(ar)) return true;
		}
		return false;
		
	}

	@Override
	public void apply(AcegiRole role) {
		role.setEnabled(true);
		getSession().merge(role);		
	}

	

	@Override
	public void notifyDone(String id) {
		notifyService.doneEvent(id);
		
	}
   
}
